package com.hoshiibuy.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.GenerationCode;
import com.hoshiibuy.purchase.entity.Po;
import com.hoshiibuy.purchase.feign.*;
import com.hoshiibuy.purchase.mapper.PoMapper;
import com.hoshiibuy.purchase.service.PoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.time.LocalDate;
import java.time.LocalDateTime;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author campchen
 * @since 2019-05-15
 */
@Service
public class PoServiceImpl extends ServiceImpl<PoMapper, Po> implements PoService {

    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private PlatformService platformService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private OrganizationService organizationService;

    /**
    * @param id
    * @return com.hoshiibuy.purchase.entity.Po
    * @description: 根据id查询
    */
    @Cacheable(cacheNames = "Po", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "采购- -查询 ")
    public Po getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
    * @param entity
    * @return boolean
    * @description: 新增
    */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Po", allEntries = true)
    @Logable(authName = "采购--新增")
    public boolean save(Po entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (id == null || id == "") {
            throw new ServiceException(CommonError.error("新增失败,id生成失败"));
        }
        Response response;
        //订单类型:1-租户采购 2-租户供应商 3-平台采购 4-平台供应商 5-店铺采购 6-店铺供应商
        switch (entity.getSource()){
            case 2:
                response=Response.data(organizationService.getByComId(entity.getVendorId()));
                if (response.getCode() != 200 || response.getData() == null){
                    throw new ServiceException(CommonError.error("新增失败,租户供应商信息有误"));
                }
            case 1:
                response=Response.data(companyService.getById(entity.getCompanyid()));
                if (response.getCode() != 200 || response.getData() == null){
                    throw new ServiceException(CommonError.error("新增失败,公司信息有误"));
                }
                break;
            case 4:
                response=Response.data(organizationService.getByPlatId(entity.getVendorId()));
                if (response.getCode() != 200 || response.getData() == null){
                    throw new ServiceException(CommonError.error("新增失败,平台供应商信息有误"));
                }
            case 3:
                response=Response.data(platformService.getById(entity.getPlatformid()));
                if (response.getCode() != 200 || response.getData() == null){
                    throw new ServiceException(CommonError.error("新增失败,平台信息有误"));
                }
                response=Response.data(companyService.getById(entity.getCompanyid()));
                if (response.getCode() != 200 || response.getData() == null){
                    throw new ServiceException(CommonError.error("新增失败,公司信息有误"));
                }
                break;
            case 6:
                response=Response.data(organizationService.getByStoreId(entity.getVendorId()));
                if (response.getCode() != 200 || response.getData() == null){
                    throw new ServiceException(CommonError.error("新增失败,店铺供应商信息有误"));
                }
                response=Response.data(organizationService.getByPlatId(entity.getVendorId()));
                if (response.getCode() != 200 || response.getData() == null){
                    throw new ServiceException(CommonError.error("新增失败,平台供应商信息有误"));
                }
                response=Response.data(companyService.getById(entity.getCompanyid()));
                if (response.getCode() != 200 || response.getData() == null){
                    throw new ServiceException(CommonError.error("新增失败,公司信息有误"));
                }
            case 5:
                response=Response.data(storeService.getById(entity.getStoreid()));
                if (response.getCode() != 200 || response.getData() == null){
                    throw new ServiceException(CommonError.error("新增失败,平台信息有误"));
                }
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + entity.getSource());
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        entity.setPoDate(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }


    /**
    * @param queryWrapper
    * @return IPage
    * @description: 分页查询
    */
    @Logable(authName = "采购-管理-分页查询")
    @Cacheable(cacheNames = "Po", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ',queryWrapper=' + #queryWrapper.toString() + ')')")
    public IPage<Po> page(IPage<Po> page, Wrapper<Po> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Po", allEntries = true)
    @Logable(authName = "采购-管理-删除")
    public boolean deleteById(Po entity) {
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }
}
